package controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import model.Subject;
import service.SubjectService;
import service.impl.SubjectServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @author: wyt
 * @date: 2022/12/19 16:12
 */
@WebServlet("/subjectListAll")
public class SubjectListAllServlet extends HttpServlet {
    SubjectService subjectService = new SubjectServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        //Json转换
        BufferedReader bufferedReader = new BufferedReader(request.getReader());
        String line = null;
        final StringBuilder json = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null) {
            json.append(line);
        }
        JSONObject jsonObject = JSON.parseObject(json.toString());

        //接收数据
        int pageIndex = Integer.parseInt(jsonObject.getString("pageIndex"));
        List<Subject> subjectList=subjectService.findByPage(pageIndex,5);

        //发送数据给前端
        JSONObject message = new JSONObject();
        message.put("subjectMsg",subjectList);
        response.getWriter().write(message.toString());
    }
}